package com.putong35.easyui.picture.picker;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;

import com.putong35.common.utils.EasyToast;
import com.putong35.easyui.ric.PictureChooseHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/29.
 */

public class AddPictureDialogFragment extends Fragment implements ActivityCompat.OnRequestPermissionsResultCallback{

    PictureChooseHelper pictureChooseHelper;

    public AddPictureDialogFragment() {
        // Required empty public constructor
    }

    public static AddPictureDialogFragment newInstance() {
        AddPictureDialogFragment fragment = new AddPictureDialogFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
        pictureChooseHelper = new PictureChooseHelper(this,
                new PictureChooseHelper.OnPictureReturnSuccessListener() {
                    @Override
                    public void OnPictureReturnSuccess(View srcView, String... urls) {
                        Log.e("urls.length", "" + urls.length);
                        if (urls != null && urls.length > 0) {
                            for (String url1 : urls) {
                                saveUrls(url1);
                            }
                            setMaxPic(pictureChooseHelper.maxPicForChoose - urls.length);
                        }
                    }
                },
                new PictureChooseHelper.OnPictureReturnFailureListener() {
                    @Override
                    public void OnPictureReturnFailure(View srcView, Throwable t, int errorNo, String strMsg) {
                        EasyToast.showToast(getActivity(), "图片上传失败！");
                        pictureChooseHelper.selectedCount = 0;
                    }
                }, 9);
    }


    List<String> urls = new ArrayList<>();
    void saveUrls(String url) {
        if (urls.size() == 0 || urls.size() < pictureChooseHelper.selectedCount) {
            urls.add(url);
        }

        if (urls.size() != 0 && urls.size() == pictureChooseHelper.selectedCount) {
            // make json
            JSONArray jsonArray= new JSONArray();
            for (String tmpUrl : urls) {
                Log.e("urlurl", "" + tmpUrl);
                JSONObject urlJso = new JSONObject();
                try {
                    urlJso.put("url", tmpUrl);
                    jsonArray.put(urlJso);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
            pictureChooseHelper.selectedCount = 0;
            Log.e("urlurl", "" + jsonArray.toString());
        }

    }


    public void setMaxPic(int size) {
        pictureChooseHelper.setMaxPic(size);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        pictureChooseHelper.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        pictureChooseHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    public void popupChooseDialog() {
        pictureChooseHelper.popupChooseDialog(getChildFragmentManager());
    }

}
